package com.kgc.novel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.novel.common.Res;
import com.kgc.novel.domain.entity.BookDiscount;
import com.kgc.novel.domain.param.BookDiscountParam;
import com.kgc.novel.service.user.BookDiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author:lklklklk
 * @since 2023-05-16
 */

@RestController
@RequestMapping("/discount")
@Slf4j
@Api(tags = "折扣接口")
public class BookDiscountController {
    @Resource
    private BookDiscountService bookDiscountService;

    @GetMapping("/list")
    @ApiOperation("获取折扣信息分页列表")
    public Res getDiscountList(BookDiscountParam bookDiscountParam) {
        LambdaQueryWrapper<BookDiscount> wrapper = Wrappers.lambdaQuery(BookDiscount.class)
                .orderByDesc(BookDiscount::getId);
        Page<BookDiscount> page = new Page<>(bookDiscountParam.getPageNo(), bookDiscountParam.getPageSize());
        Page<BookDiscount> discountPage = bookDiscountService.page(page, wrapper);
        return Res.returnData(discountPage.getRecords(), discountPage.getTotal());
    }
    @DeleteMapping("/del/{discountId}")
    @ApiOperation("删除折扣信息")
    public Res deleteDiscount(@PathVariable(value = "discountId") Integer discountId) {
        return bookDiscountService.removeById(discountId) ? Res.returnMsg("删除折扣信息成功！") : Res.returnMsg("删除折扣信息失败！");
    }

    @DeleteMapping("/delBatch")
    @ApiOperation("批量删除折扣信息")
    public Res deleteDiscountBatch(@RequestBody List<Integer> ids) {
        return bookDiscountService.removeBatchByIds(ids) ? Res.returnMsg("批量删除用户数据成功！") : Res.returnMsg("批量删除用户数据失败！");
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("保存或修改折扣信息")
    public Res addOrUpdate(@RequestBody BookDiscount bookDiscount) {
        String id=bookDiscount.getId();
        if (bookDiscountService.saveOrUpdate(bookDiscount))
            return id == null ? Res.returnMsg("添加折扣信息成功！") : Res.returnMsg("修改折扣信息成功！");
        return id == null ? Res.returnErrMsg("添加折扣信息失败！") : Res.returnErrMsg("修改折扣信息失败！");
    }

}
